﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Linq;
using System.Text;
using System.IO;
using Mimoza.Common;

namespace Mimoza.ObjectLayer
{
    public class AdapterInfo
    {
        public int AdapterID;
        public bool EnableDHCP4IP;
        public string IPAddress;
        public string SubNetMask;
        public string DefaultGateWay;
        public bool EnableDHCP4DNS;
        public string DNS1;
        public string DNS2;
    }
    public class Host : INotifyPropertyChanged
    {
        private OL ol;
        private HostRecord record;

        public Host(OL ol)
        {
            this.ol = ol;
            this.record = new HostRecord();
        }
        public Host(OL ol, HostRecord record)
        {
            this.ol = ol;
            this.record = record;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public HostRecord Record
        {
            get
            {
                return record;
            }
        }
        public Guid ID 
        {
            get { return record.ID; }
        }
        public Container Parent
        {
            get
            {
                if (record.ParentID != null)
                {
                    return ol.ContainerRepository.GetContainerByID(record.ParentID);
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (value.Record.ID != this.record.ParentID)
                {
                    record.ParentID = value.ID;
                    NotifyPropertyChanged("Parent");
                }
            }
        }
        public string Name
        {
            get { return record.Name; }
            set
            {
                if (value != this.record.Name)
                {
                    record.Name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }
        public string NewName
        {
            get { return record.NewName; }
            set
            {
                if (value != this.record.NewName)
                {
                    record.NewName = value;
                    NotifyPropertyChanged("NewName");
                }
            }
        }
        public string Description
        {
            get { return record.Description; }
            set
            {
                if (value != this.record.Description)
                {
                    record.Description = value;
                    NotifyPropertyChanged("Description");
                }
            }
        }
        public string MACAddress
        {
            get { return record.MACAddress; }
            set
            {
                if (value != this.record.MACAddress)
                {
                    record.MACAddress = value;
                    NotifyPropertyChanged("MACAddress");
                }
            }
        }
        public string IPAddress
        {
            get { return record.IPAddress; }
            set
            {
                if (value != this.record.IPAddress)
                {
                    record.IPAddress = value;
                    NotifyPropertyChanged("IPAddress");
                }
            }
        }        
        public AdapterInfo[] AdaptersInfo
        {
            get 
            {
                if (null != this.record.AdaptersInfo && this.record.AdaptersInfo.Length > 0)
                {
                    XmlSerializer deserializer = new XmlSerializer(typeof(AdapterInfo[]));
                    using (TextReader text = new StringReader(this.record.AdaptersInfo))
                    {
                        return (AdapterInfo[])deserializer.Deserialize(text);
                    }
                }
                return null;
            }
            set 
            {   string val = null;

                if (null != value)
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(AdapterInfo[]));
                    TextWriter text = new StringWriter();
                    serializer.Serialize(text, value);
                    val = text.ToString();
                }

                if (val != this.record.AdaptersInfo)
                {
                    record.AdaptersInfo = val;
                    NotifyPropertyChanged("AdaptersInfo");
                }
            }
        }

        public string OS
        {
            get { return record.OS; }
            set
            {
                if (value != this.record.OS)
                {
                    record.OS = value;
                    NotifyPropertyChanged("OS");
                }
            }
        }
        public string Model
        {
            get { return record.Model; }
            set
            {
                if (value != this.record.Model)
                {
                    record.Model = value;
                    NotifyPropertyChanged("Model");
                }
            }
        }
        public string Version
        {
            get { return record.Version; }
            set
            {
                if (value != this.record.Version)
                {
                    record.Version = value;
                    NotifyPropertyChanged("Version");
                }
            }
        }
        public DBHostStatus Status
        {
            get { return record.Status; }
            set
            {
                if (value != this.record.Status)
                {
                    record.Status = value;
                    NotifyPropertyChanged("Status");
                }
            }
        }
        public Guid ModificationID
        {
            get { return record.ModificationID; }
            set
            {
                if (value != this.record.ModificationID)
                {
                    record.ModificationID = value;
                    NotifyPropertyChanged("ModificationID");
                }
            }
        }
        public DateTime UpdateDate
        {
            get { return record.UpdateDate; }
        }
        public DateTime LastConnectionDate
        {
            get 
            { 
                return record.LastConnectionDate; 
            }
            set 
            {
                if (value != this.record.LastConnectionDate)
                {
                    record.LastConnectionDate = value;
                    NotifyPropertyChanged("LastConnectionDate");
                }
            }
        }
        public bool IsActive
        {
            get
            {
                if ((DateTime.Now - record.LastConnectionDate).TotalSeconds > Common.GlobalSettings.c_DefaultPingTimeout)
                    return false;
                else return true;
            }

        }
    }
}
